package cn.jiedh.app.meiweidk.Utils.tool;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;

/**
 * @name：
 * @author： 周毅
 * @phone： 15826080712
 * @createTime： 2017/5/26
 * @modifyTime： 2017/5/26
 * @explain：
 */


/**
 * 网络判断工具类
 *
 * @author http://xiaxveliang.blog.163.com
 *
 */
public final class NetworkUtils {

    /**
     * 判断网络链接是否OK(包括WIFI,ETHERNET,GPRS等)
     *
     * @param mContext
     * @return
     */
    public static boolean isConnect(Context context) {
        if (context == null) {
            return false;
        }
        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cm == null) {
            return false;
        }
        NetworkInfo ni = cm.getActiveNetworkInfo();
        if (ni == null) {
            return false;
        }
        if (ni.getState() == State.CONNECTED) {
            return true;
        }
        return false;

    }

    /**
     * WIFI,ETHERNET链接正常
     *
     * @param mContext
     * @return
     */
    public static boolean isWifiEthernetConnect(Context context) {

        if (context == null) {
            return false;
        }
        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cm == null) {
            return false;
        }
        NetworkInfo ni = cm.getActiveNetworkInfo();
        if (ni == null) {
            return false;
        }
        if (ni.getState() == State.CONNECTED
                && (ni.getType() == ConnectivityManager.TYPE_WIFI || ni
                .getType() == ConnectivityManager.TYPE_ETHERNET)) {
            return true;
        }

        return false;
    }

    /**
     * GPRS(2G,3G,4G)
     *
     * @param mContext
     * @return
     */
    public static boolean isMobileConnect(Context context) {

        if (context == null) {
            return false;
        }
        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cm == null) {
            return false;
        }
        NetworkInfo ni = cm.getActiveNetworkInfo();
        if (ni == null) {
            return false;
        }
        if (ni.getState() == State.CONNECTED
                && ni.getType() == ConnectivityManager.TYPE_MOBILE) {
            return true;
        }
        return false;
    }

}
